//
//  ViewController.m
//  UI-轮播图的实现
//
//  Created by 张哲炜 on 2018/8/26.
//  Copyright © 2018年 张哲炜. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    //最好先实现全局变量
    UIScrollView * scrollView;
    NSArray * array;
    UIPageControl * pageControl;
    int totalPage;
    int curPage;
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    array = [NSArray arrayWithObjects:@"4.jpeg", @"1.jpeg", @"2.jpeg", @"3.jpeg", @"4.jpeg",@"1.jpeg",nil];
    
    
    scrollView =[[UIScrollView alloc]initWithFrame:(CGRectMake(0, 0,MAIN_SCREEN_WIDTH , 200))];
    //scrollView的水平滚动条隐藏
    scrollView.showsVerticalScrollIndicator =NO;
    //scrollView的竖直滚动条隐藏
    scrollView.showsHorizontalScrollIndicator = NO;
    //scrollView 的整夜翻动
    scrollView.pagingEnabled=YES;
    //签订代理协议
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    
     pageControl =[[UIPageControl alloc]initWithFrame:(CGRectMake(0, 180, MAIN_SCREEN_WIDTH, 10))];
    //设置pagecontrol的颜色
    pageControl.pageIndicatorTintColor =[UIColor grayColor];
    pageControl.currentPageIndicatorTintColor =[UIColor  blueColor];
    //设置pagecontrol的数量
    pageControl.numberOfPages =4;
    [self.view addSubview:pageControl];
    
    
    
    
    totalPage =[self->array count];
    
    for(int i =0;i<totalPage;i++){
        
        UIImageView *imageView =[[UIImageView alloc]initWithFrame:(CGRectMake(i*MAIN_SCREEN_WIDTH, 0, MAIN_SCREEN_WIDTH, 200))];
        
        [imageView setImage:[UIImage imageNamed:[array objectAtIndex:i]]];
        [scrollView addSubview:imageView];
        
    }
    
    scrollView. contentSize = CGSizeMake(MAIN_SCREEN_WIDTH*totalPage, 200);
    [ scrollView setContentOffset:CGPointMake(MAIN_SCREEN_WIDTH, 0) animated:NO];
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
//scrollVIew已经结束滚动
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    [self scrollViewFinsh:scrollView];
    
}
//scrollView已经结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
     [self scrollViewFinsh:scrollView];
    
}


-(void)scrollViewFinsh:(UIScrollView *)scrollView{
    
    CGFloat scrollWidth = self->scrollView.frame.size.width;
    NSInteger index = self->scrollView.contentOffset.x / scrollWidth;
    
    //NSLog(@"index=%d", index);
    if (index == 5) {
        //显示最后一张的时候，强制设置为第二张（也就是轮播图的第一张），这样就开始无限循环了
        [self->scrollView setContentOffset:CGPointMake(scrollWidth, 0) animated:NO];
    } else if (index == 0) {
        //显示第一张的时候，强制设置为倒数第二张（轮播图最后一张），实现倒序无限循环
        [self->scrollView setContentOffset:CGPointMake(4* scrollWidth, 0) animated:NO];
    }
    
}
//使page control随之图片的移动而移动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollWidth = self->scrollView.frame.size.width;
    NSInteger index = self->scrollView.contentOffset.x / scrollWidth;

    if (index == 5) {
        index = 1;
    } else if (index == 0) {
        index = 3;
    }
    self->pageControl.currentPage = index - 1;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
